package com.guoguo.utils;

import com.aliyun.oss.OSSClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.guoguo.utils.aliOss.BaseAliUtils;
import org.apache.commons.collections.map.HashedMap;

import java.util.Map;

/**
 * Created by Administrator on 2017/11/22 0022.
 */

     /*
                           _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                          O\  =  /O
                       ____/`---'\____
                     .'  \\|     |//  `.
                    /  \\|||  :  |||//  \
                   /  _||||| -:- |||||-  \
                   |   | \\\  -  /// |   |
                   | \_|  ''\---/''  |   |
                   \  .-\__  `-`  ___/-. /
                 ___`. .'  /--.--\  `. . __
              ."" '<  `.___\_<|>_/___.'  >'"".
             | | :  `- \`.;`\ _ /`;.`/ - ` : | |
             \  \ `-.   \_ __\ /__ _/   .-` /  /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 佛祖保佑       永无BUG
        */
public class AliVodUtils extends BaseAliUtils {

    private static DefaultAcsClient defaultAcsClient;

    /**
     * 初始化参数
     */
    private static void initConfig() {
        if (bucketName == null || "".equals(bucketName)) {
            bucketName = PropertyUtils.getProperty(configFileName, "ali.bucketName");
        }
        if (accessKeyId == null || "".equals(accessKeyId)) {
            accessKeyId = PropertyUtils.getProperty(configFileName, "ali.accessKeyId");
        }
        if (endpoint == null || "".equals(endpoint)) {
            endpoint = PropertyUtils.getProperty(configFileName, "ali.vod.url");
        }
        if (accessKeySecret == null || "".equals(accessKeySecret)) {
            accessKeySecret = PropertyUtils.getProperty(configFileName, "ali.accessKeySecret");
        }
    }

    /**
     * 单例返回上传对象
     *
     * @return
     */
    private static DefaultAcsClient getDefaultAcsClient() {
        initConfig();
        if (defaultAcsClient == null) {
            return new DefaultAcsClient(DefaultProfile.getProfile(endpoint, accessKeyId, accessKeySecret));
        }
        return defaultAcsClient;
    }

    public  static void uploadVideo(){

    }

    /**
     * 获取视频上传凭证和地址
     *
     * @param fileName 文件名
     * @param title    视频标题
     * @param tags     视频标签，多个用逗号分隔
     * @param desc     视频描述
     * @return
     */
    public static Map<String, String> createUploadVideo(String fileName, String title, String tags, String desc) {
        Map<String, String> uploadConfig = new HashedMap();
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        CreateUploadVideoResponse response = null;
        try {
              /*必选，视频源文件名称（必须带后缀, 支持 ".3gp", ".asf", ".avi", ".dat", ".dv", ".flv", ".f4v", ".gif", ".m2t", ".m3u8", ".m4v", ".mj2", ".mjpeg", ".mkv", ".mov", ".mp4", ".mpe", ".mpg", ".mpeg", ".mts", ".ogg", ".qt", ".rm", ".rmvb", ".swf", ".ts", ".vob", ".wmv", ".webm"".aac", ".ac3", ".acm", ".amr", ".ape", ".caf", ".flac", ".m4a", ".mp3", ".ra", ".wav", ".wma"）*/
            request.setFileName(fileName);
            //必选，视频标题
            request.setTitle(title);
            //可选，分类ID
            request.setCateId(0);
            //可选，视频标签，多个用逗号分隔
            request.setTags(tags);
            //可选，视频描述
            request.setDescription(desc);
            response = getDefaultAcsClient().getAcsResponse(request);
        } catch (ServerException e) {
            System.out.println("CreateUploadVideoRequest Server Exception:");
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            System.out.println("CreateUploadVideoRequest Client Exception:");
            e.printStackTrace();
            return null;
        }

        uploadConfig.put("requestId:", response.getRequestId());
        uploadConfig.put("uploadAuth:", response.getUploadAuth());
        uploadConfig.put("videoId:", response.getVideoId());
        uploadConfig.put("uploadAddress:", response.getUploadAddress());
        return uploadConfig;
    }

    /**
     * 刷新视频上传凭证
     *
     * @param videoId
     */
    public static Map<String, String> refreshUploadVideo(String videoId) {
        Map<String, String> uploadConfig = new HashedMap();
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        RefreshUploadVideoResponse response = null;
        try {
            request.setVideoId(videoId);
            response = getDefaultAcsClient().getAcsResponse(request);
        } catch (ServerException e) {
            System.out.println("RefreshUploadVideoRequest Server Exception:");
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            System.out.println("RefreshUploadVideoRequest Client Exception:");
            e.printStackTrace();
            return null;
        }
        uploadConfig.put("requestId:", response.getRequestId());
        uploadConfig.put("uploadAuth:", response.getUploadAuth());
        return uploadConfig;
    }
}
